﻿/******************************************************************************
* 【本类功能概述】                                 					      *
 * 版权所有（C）2017-20XX，米兔网络*
* 保留所有权利。                                                            *
******************************************************************************
* 作者 : <hughjk>
 * 创建时间:2018年5月3日 14:30:24
 * 文件描述:行为群组
*****************************************************************************/

using System;

namespace BrainSpace
{
    using System.Collections.Generic;

    //行为群组
    public class ActionCluster
    {
        public Goal m_steps;
        public ActionClusteData m_data;

        public List<Actions> m_ActionCluster = new List<Actions>();
        public Action<int> Over;

        public bool isOver = true;
        private int curSerialId = 0;//当前串行ID
        public int SelectId = 0;//当前选择执行ID

        //构建
        public ActionCluster(Goal steps, ActionClusteData data, Action<int> OverCallBack)
        {
            m_steps = steps;
            m_data = data;
            Over = OverCallBack;
        }

        //初始化
        public void Init()
        {
            for (int i = 0; i < m_data.m_ListData.Count; i++)
            {
                var ex = m_data.m_ListData[i];
                var actions = GoalPlant.CreateActions(m_data.m_ActionsType, ex.m_id);
                actions.Init(this, i, ex);
                m_ActionCluster.Add(actions);
            }
            curSerialId = 0;
        }

        //开始执行
        public void Start()
        {
            if (m_steps.IsUse == false) return;
            isOver = false;
            debug.log(m_steps.isdebug, m_steps.m_data.m_id, m_data.m_ActionsType.toName(), "开始");
            switch (m_data.exType)
            {
                case ClusterExecuteType.Parallel:
                    ParallelStart();
                    break;

                case ClusterExecuteType.Serial:
                    SerialStart();
                    break;

                case ClusterExecuteType.Select:
                    SelectStart();
                    break;
            }
        }

        //并行开始
        private void ParallelStart()
        {
            for (int i = 0; i < m_ActionCluster.Count; i++)
            {
                m_ActionCluster[i].Start();
            }
        }

        //串行开始
        private void SerialStart()
        {
            if (curSerialId < m_ActionCluster.Count)
            {
                m_ActionCluster[curSerialId].Start();
                curSerialId++;
            }
            else
            {
                SerialOvers();
            }
        }

        //选择执行开始
        private void SelectStart()
        {
            m_ActionCluster[SelectId].Start();
        }

        //当节点结束
        public void Overs(int exId)
        {
            if (m_steps.IsUse == false || isOver == true) return;
            SelectId = exId;

            switch (m_data.exType)
            {
                case ClusterExecuteType.Parallel:
                    ParallelOvers();
                    break;

                case ClusterExecuteType.Serial:
                    SerialStart();
                    break;

                case ClusterExecuteType.Select:
                    ActionClusterOver();
                    break;
            }
        }

        //并行结束
        private void ParallelOvers()
        {
            if (m_data.IsAll)
            {
                for (int i = 0; i < m_ActionCluster.Count; i++)
                {
                    if (m_ActionCluster[i].isOver == false)
                    {
                        return;
                    }
                }
            }
            ActionClusterOver();
        }

        //串行结束
        private void SerialOvers()
        {
            ActionClusterOver();
        }

        //群组整体结束
        private void ActionClusterOver()
        {
            debug.log(m_steps.isdebug, m_steps.m_data.m_id, m_data.m_ActionsType.toName(), "结束");
            isOver = true;
            Over(SelectId);
        }

        //打断动作序列
        public void BreakOvers()
        {
            isOver = true;
            for (int i = 0; i < m_ActionCluster.Count; i++)
            {
                if (m_ActionCluster[i] != null && m_ActionCluster[i].isOver == false)
                {
                    m_ActionCluster[i].Over();
                    m_ActionCluster[i].Dispose();
                    m_ActionCluster[i] = null;
                }
            }
        }

        public string ActTypeName(int actiontype)
        {
            string s = string.Empty;
            switch (m_data.m_ActionsType)
            {
                case ActionsType.condition:
                    s = actiontype.toEnum<ConditionType>().toName();
                    break;

                case ActionsType.execute:
                    s = actiontype.toEnum<ExecuteType>().toName();
                    break;
            }
            return s;
        }

        public void Dispose()
        {
            for (int i = 0; i < m_ActionCluster.Count; i++)
            {
                if (m_ActionCluster[i] != null)
                {
                    m_ActionCluster[i].Dispose();
                    m_ActionCluster[i] = null;
                }
            }
        }
    }
}